package com.mc.servlet;

import com.mc.bean.Teacher;
import com.mc.service.TeacherService;
import com.mc.service.impl.TeacherServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author wangcf
 * @version 1.0
 * @Description 教师修改处理器
 * @date 2023/2/14 18:52
 */
@WebServlet("/teacherupdate.action")
public class TeacherUpdateServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        TeacherService teacherService = new TeacherServiceImpl();
        String type = req.getParameter("type");
        System.out.println(type);
        String tid = req.getParameter("tid");
        Teacher teacher = teacherService.findByTid(Integer.parseInt(tid));
        if (type.equals("1")) {
            req.setAttribute("teacher", teacher);
            req.getRequestDispatcher("teacherupdate.jsp").forward(req, resp);
        } else if (type.equals("2")) {
            String name = req.getParameter("name");
            String sex = req.getParameter("sex");
            String ageStr = req.getParameter("age");
            int age = Integer.parseInt(ageStr);
            System.out.println(age);
            String tel = req.getParameter("tel");
            if (name != null) {
                teacher.setTname(name);
            }
            if (sex != null) {
                teacher.setTsex(sex);
            }
            if (ageStr != null) {
                teacher.setTage(age);
            }
            if (tel != null) {
                teacher.setTtel(tel);
            }
            System.out.println(teacher);
            boolean flag = teacherService.update(teacher);
            System.out.println(flag);
            resp.sendRedirect("teacherlist.action");
        } else {
            System.out.println("错误");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
